package org.github.manx98.jlibhac.tools.fssystem.ncautils;

import org.github.manx98.jlibhac.common.math.IByteUtil;

/**
 * @author Manx98
 * @version 1.0
 * @since 2022/10/4 0:31
 */
public class NcaFsIntegrityInfoIvfc {
    private final byte[] data;
    IvfcStruct ivfcStruct = new IvfcStruct();
    public NcaFsIntegrityInfoIvfc(byte[] data) {
        this.data = data;
        IByteUtil.mapToStruct(ivfcStruct, data, 0);
    }

    public int getLevelCount() {
        return ivfcStruct.LevelCount;
    }

    public byte[] getMasterHash() {
        return IByteUtil.slice(data, IvfcStruct.MasterHashOffset, ivfcStruct.MasterHashSize);
    }

    public long getLevelOffset(int index) {
        return getLevelInfo(index).Offset;
    }

    public IvfcLevel getLevelInfo(int index) {
        validateLevelIndex(index);
        IvfcLevel ivfcLevel = new IvfcLevel();
        int offset = IvfcStruct.IvfcLevelsOffset + IvfcLevel.IvfcLevelSize * index;
        IByteUtil.mapToStruct(ivfcLevel, data, offset);
        return ivfcLevel;
    }

    private void validateLevelIndex(int index) {
        if (index < 0 || index > 6)
        {
            throw new IllegalArgumentException("IVFC level index must be between 0 and 6. Actual: "+index);
        }
    }

    public long getLevelSize(int index) {
        return getLevelInfo(index).Size;
    }
}
